# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import time

from django.shortcuts import render

from tools.rrd_graph import RrdGraph


def index(request):
    return render(request, "index.html")


def query(request):
    # url example: /netgraph/query?start=2017-04-01&end=2017-04-11
    starttime = request.GET.get('start', '-1d')
    endtime = request.GET.get('end', 'now')
    if starttime != '-1d':
        starttimearray = time.strptime(starttime, '%Y-%m-%d')
        starttime = time.strftime("%m/%d/%Y", starttimearray)
    if endtime != 'now':
        endtimearray = time.strptime(endtime, '%Y-%m-%d')
        endtime = time.strftime("%m/%d/%Y", endtimearray)
    data = RrdGraph(starttime, endtime).run()
    return render(request, "index.html", {'data': data})
